package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public List<ItemCat> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", 0);
        List<ItemCat> cats = itemCatMapper.selectList(queryWrapper);
        for (ItemCat cat : cats) {
            Integer id = cat.getId();
            QueryWrapper qw = new QueryWrapper();
            qw.eq("parent_id", id);
            List<ItemCat> cats1 = itemCatMapper.selectList(qw);
            cat.setChildren(cats1);
            for (ItemCat cat1 : cats1) {
                Integer id1 = cat1.getId();
                QueryWrapper qw1 = new QueryWrapper();
                qw.eq("parent_id", id1);
                List<ItemCat> cats11 = itemCatMapper.selectList(qw1);
                cat.setChildren(cats11);
            }
        }
        return cats;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer type) {
        List<ItemCat> cats = itemCatMapper.selectList(null);
        Map<Integer, List<ItemCat>> map = this.getMap(cats);
        if (type == 1) return map.get(0);
        if (type == 2) return this.findType2ItemCat(map);
        return this.findType3ItemCat(map);
    }

    @Override
    public void updatestatus(Integer id, Boolean status) {
        ItemCat cat = new ItemCat();
        cat.setStatus(status);
        QueryWrapper qw = new QueryWrapper();
        qw.eq("id", id);
        itemCatMapper.update(cat, qw);
    }

    @Override
    public Integer insertItemCat(ItemCat cat) {
        cat.setStatus(true);
        return itemCatMapper.insert(cat);
    }

    @Override
    public Integer deleteItemCat(Integer id, Integer level) {
        if (level == 1) {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("parent_id", id);
            List<ItemCat> cats = itemCatMapper.selectList(qw);
            for (ItemCat cat : cats) {
                QueryWrapper qw1 = new QueryWrapper();
                qw1.eq("parent_id", cat.getId());
                itemCatMapper.delete(qw1);
                itemCatMapper.deleteById(cat.getId());
            }

        }
        if (level == 2) {
            QueryWrapper qw = new QueryWrapper();
            qw.eq("parent_id", id);
            itemCatMapper.delete(qw);
        }

        return itemCatMapper.deleteById(id);
    }

    @Override
    public Integer updateItemCat(ItemCat cat) {
        return itemCatMapper.updateById(cat);
    }

    private List<ItemCat> findType3ItemCat(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> onelist = map.get(0);
        for (ItemCat item : onelist) {
            List<ItemCat> twolist = map.get(item.getId());
            if (twolist == null)
                continue;
            for (ItemCat item2 : twolist) {
                item2.setChildren(map.get(item2.getId()));
            }
            item.setChildren(map.get(item.getId()));
        }
        return onelist;
    }

    public List<ItemCat> findType2ItemCat(Map<Integer, List<ItemCat>> map) {
        List<ItemCat> onelist = map.get(0);
        for (ItemCat item : onelist) {
            item.setChildren(map.get(item.getId()));
        }
        return onelist;
    }

    public Map<Integer, List<ItemCat>> getMap(List<ItemCat> cats) {
        Map<Integer, List<ItemCat>> map = new HashMap<>();
        for (ItemCat cat : cats) {
            if (map.containsKey(cat.getParentId())) {
                map.get(cat.getParentId()).add(cat);
            } else {
                List<ItemCat> list = new ArrayList<>();
                list.add(cat);
                map.put(cat.getParentId(), list);
            }
        }
        return map;
    }
}
